package com.io.webflow.controller;

import com.io.config.WrapInt;
import com.io.model.RegisterReq;
import com.io.model.RegisterResp;
import com.io.protocol.CommandConstants;
import com.io.util.ValidationUtils;
import com.io.webflow.service.ChannelService;
import com.io.webflow.service.RegisterService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * Created by yangyx on 2016/7/28.
 *该controller是终端发送消息的controller，非视图controller
 */
@Controller
public class TerminalController {

    static final Logger logger= LoggerFactory.getLogger(TerminalController.class);

    @Autowired
    RegisterService registerService;
    @Autowired
    ChannelService channelService;



    /**
     *@Author yangyx 【yang_yaxu@launchdesign.com.cn】
     *@Date  2016/7/28 10:06
     *根据传过来的用户信息在用户表内进行注册
     */
    public void onRegisterCar(ChannelHandlerContext ctx, RegisterReq obj){
        RegisterResp outObj=new RegisterResp();
        try{
            //根据正则表达式验证vin，tid，password
            if(ValidationUtils.isValidVin(obj.vin)&&ValidationUtils.isValidTid(obj.terminalID)
                    &&ValidationUtils.isValidPassword(obj.password)) {

                //根据传的的对象和ctx参数进行处理之后再出参一个RegisterReq对象
                WrapInt<RegisterReq> result = registerService.terminalRegister(obj);

                if(result.getIntVal()==CommandConstants.RespResCode_Success){
                    channelService.delRepeatMsg(ctx,result);
                    outObj.setResult((byte) result.getIntVal());
                }else{
                    outObj.setResult((byte) result.getIntVal());
                }
            }else{
                outObj.setResult(CommandConstants.RespResCode_InvalidParameter);
            }
        }catch (Exception e){
            outObj.setResult(CommandConstants.RespResCode_ServerError);
        }finally {
            ctx.write(outObj);
        }
    }

    public void changepassword(ChannelHandlerContext ctx, RegisterReq obj){

    }


}
